package com.paul.sys.tools;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.math.BigDecimal;

public class Util {
	private static Log log = LogFactory.getLog(Util.class);

	// 五十公里。
	private static BigDecimal DISTANCE50 = new BigDecimal(5000000);
	private static BigDecimal DISTANCE100 = new BigDecimal(10000000);
	
	private final static int scale = 15 ;
	private final static int rould = BigDecimal.ROUND_HALF_UP ;
	
	private static   double R_ = 6378136.49;
	private static   double PI_m_R_ = Math.PI* R_;
	
	private static   BigDecimal R = new BigDecimal(6378136.49);
	private static   BigDecimal  PI = new BigDecimal(Math.PI);
	private static   BigDecimal PI_m_R = PI.multiply(R);
	private static   BigDecimal PI_d_90 = PI.divide(new BigDecimal(90), scale, rould );
	private static   BigDecimal PI_m_R_d_180 = PI_m_R.divide(new BigDecimal(180), scale, rould );

	public static long getDistanceDecimal(double lon1, double lat1, double lon2, double lat2) {
		//double R = 6378136.49;
		//double PI = Math.PI;
		BigDecimal d, y, x;
		double res = 0.0d;
		
		x = new BigDecimal(lon2 - lon1).multiply(PI_d_90.multiply(new BigDecimal(Math.cos((lat1 + lat2))) )).multiply(PI_m_R_d_180) ;
		System.out.println("  distanceBigDecimal  ====   x:  " +x.doubleValue() );		
		y =  PI_m_R_d_180.multiply((new BigDecimal(lat2 - lat1) ));
		System.out.println("  distanceBigDecimal  ====   y:  "  +y.doubleValue() );		
		d =  x.pow(2).add( y.pow(2));
		System.out.println("  distanceBigDecimal  ====   d:  " +d.doubleValue() );		
		res = Math.sqrt(d.doubleValue() );
		System.out.println("  distanceBigDecimal  ==== res:  "+ res  );		
		return Math.round(res * 100);
	}
	
	
	// 获得两点间距离
	public static long getDistance(double lon1, double lat1, double lon2, double lat2) {
		double R = 6378136.49;
		double PI = Math.PI;
		double d, y, x;
		x = (lon2 - lon1) * PI * R * Math.cos(((lat1 + lat2) / 2) * PI / 180) / 180;
		y = (lat2 - lat1) * PI * R / 180;
		d = Math.sqrt(x * x + y * y);
		return Math.round(d);
	}
	 
	// 获得两点间距离
	public static long getDistance3(double lon1, double lat1, double lon2, double lat2) {
		double R = 6378136.49;
		double PI = Math.PI;
		double d, y, x;
		x = (lon2 - lon1)  * Math.cos(((lat1 + lat2) / 2) * PI / 180) * PI * R / 180;
		System.out.println("  getDistance3  ==== x:  " +x  );
		y = (lat2 - lat1) * PI * R / 180;
		System.out.println("  getDistance3  ==== y  "  +y);
		d = Math.sqrt(x * x + y * y);
		System.out.println("  getDistance3  ==== d  "  +d );
		return Math.round(d * 100);
	}
	
	  
	public static void main(String[] args) {
		long distance2 = getDistance(109.5303,18.281488, 109.5082, 18.293581);
		long distanceBigDecimal = getDistanceDecimal(109.5303,18.281488, 109.5082, 18.293581);
		//BigDecimal
		System.out.println(distance2);
		System.out.println(distanceBigDecimal);
	}
	 
	
	

 
}
